<?php
namespace Sys\Controller;
use Common\Controller\BaseController;
class IndexController extends BaseController {
	public function __construct(){
		Parent::__construct();
	}
	private function page(){
		$menu = M('menus'); 
		$res = $menu->page($_GET['p'],'5')->select();
		$count = $menu->count();
		$Page  = new \Think\Page($count,5);
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('header','%UP_PAGE% %FIRST% %LINK_PAGE% %END% %DOWN_PAGE%');
		$Page->setConfig('theme','%HEADER%');
		$show       = $Page->show();
		$this->assign('page',$show);
		$this-> assign('menus', $res);
	}
	public function index(){
		$this->page();
		$where = [
			'up' => '0',
		];
	    $up = M('menus')-> where($where)->select();
		
		$this-> assign('opts', $up);
		$this-> assign('json_options',json_encode($up));
		$this -> display();
	}
	public function doDelMenu(){
		$ids = I('post.ids');
		$taf = M('menus')-> delete($ids);
		if(!$taf){
			$msg = [
				'msg' 	=> '删除失败',
				'error' => 0,
			];

		}
	}
	public function doEditMenu(){
		$id=I('post.menu_id');
		$case= M('menus')->where(['menu_id'=>$id])->select();
		if($case){
			if(M('menus')-> create()){
			M('menus')->save();
			$msg = [
				'msg' 	=> '修改失败',
				'error' => 0,
			];
		}}else{
			if(M('menus')-> create()){
			M('menus')->add();
			$msg = [
				'msg' 	=> '添加失败',
				'error' => 0,
			];

		}

		}
$this->ajaxReturn($msg);		
	}
	public function lastId(){
		$lastId=M('menus')->max('menu_id');
		$this->ajaxReturn($lastId);
	} 


}
	

